package cn.ljh.controller;

import cn.ljh.entity.Foods;
import cn.ljh.service.FoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/foods")
public class FoodsController {

    @Autowired
    private FoodsService foodsService;

    @RequestMapping("/list")
    public String list(@RequestParam(value = "name", required = false) String name,
                       @RequestParam(value = "type", required = false) String type,
                       Model model) {
        Map<String, Object> condition = new HashMap<>();
        condition.put("name", name);
        condition.put("type", type);

        List<Foods> foodsList = foodsService.getFoodsByCondition(condition);
        model.addAttribute("foodsList", foodsList);
        model.addAttribute("total", foodsList.size());
        model.addAttribute("name", name);
        model.addAttribute("type", type);

        return "foodsList";
    }

    @RequestMapping("/delete")
    public String delete(@RequestParam("id") Integer id) {
        foodsService.deleteFoods(id);
        return "redirect:list";
    }
}    